<template>
	<view>
		<uni-nav-bar height="120rpx" statusBar="true" backgroundColor="#6869a8" fixed="true">
			<view class="" slot="left" @click="back">
				<uni-icons type="back" size="30" color="#FFF"></uni-icons>
			</view>
			<view class="tab">
				<text class="btn ren" @click="tab = 1" :class="[tab===1?'isActive':'noActive']">找人</text>
				<text class="btn qun" @click="tab = 2" :class="[tab === 2?'isActive':'noActive']">找群</text>
			</view>
		</uni-nav-bar>
		<uni-search-bar v-if="tab === 1" :radius="20" @confirm="search" v-model="searchKeywords_ren" class="searchbar"
			placeholder="ID号/昵称" bgColor="#f5f5f5"></uni-search-bar>
		<uni-search-bar v-if="tab === 2" :radius="20" @confirm="search" v-model="searchKeywords_qun" class="searchbar"
			placeholder="群号/群名称" bgColor="#f5f5f5"></uni-search-bar>

		<view class="list" v-if="tab === 1">
			<view class="card" v-for="(item,index) in person_list" :key="index">
				<image :src="item.avatar" mode="aspectFill" class="image" @click="watchFriend(item.id)"></image>
				<view class="text username" @click="watchFriend(item.id)">
					<rich-text :nodes="toBlue(item.username,loadKeyword_ren)"></rich-text>
				</view>
				<view class="text id">
					<rich-text :nodes="toBlue('('+item.id+')',loadKeyword_ren)"></rich-text>
				</view>
				<view class="button" hover-class="button_hover" @click="addFriendId = item.id">添加</view>
			</view>
			<u-modal :show="addFriendId>0" title="好友验证消息" width="500rpx" :showCancelButton="true"
				@cancel="addFriendId = -1" @confirm="addFriend">
				<view>
					<u-input v-model="addFriendMessage" />
				</view>
			</u-modal>
			<view class="noInfo">
				{{person_message}}
			</view>
		</view>
		<view class="list" v-if="tab === 2">
			<view class="card" v-for="(item,index) in group_list" :key="index">
				<image :src="item.avatar" mode="aspectFill" class="image" @click="watchGroup(item.id)"></image>
				<view class="text username" @click="watchGroup(item.id)">
					<rich-text :nodes="toBlue(item.groupName,loadKeyword_qun)"></rich-text>
				</view>
				<view class="text id">
					<rich-text :nodes="toBlue('('+item.id+')',loadKeyword_qun)"></rich-text>
				</view>
				<!-- <view class="des"><text v-if="item.description!=''">{{item.description}}</text><text
						v-else>没有群描述...</text></view> -->
				<view v-if="item.needVerify === 0" class="button" hover-class="button_hover"
					@click="joinGroup(item.id)">加入</view>
				<view v-if="item.needVerify === 1" class="button" hover-class="button_hover"
					@click="addGroupId = item.id">申请</view>
				<view v-if="item.needVerify === 2" class="no">不可加入</view>
			</view>
			<u-modal :show="addGroupId>0" title="群验证消息" width="500rpx" :showCancelButton="true"
				@cancel="addGroupId = -1" @confirm="applyGroup">
				<view>
					<u-input v-model="addGroupMessage" />
				</view>
			</u-modal>
			<view class="noInfo">
				{{group_message}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadKeyword_ren: "", //记录用户检索keywords,保证加载后续数据时,关键词不变
				loadKeyword_qun: "",
				searchKeywords_ren: "",
				searchKeywords_qun: '',
				tab: 1,
				person_noMore: false,
				person_message: "输入关键词搜索",
				person_index: 1, //用户page
				group_noMore: false, //
				group_message: "输入关键词搜索",
				group_index: 1, //群组page
				person_list: [], //检索用户结果
				group_list: [], //检索群组结果
				addFriendId: -1,
				addFriendMessage: "我是" + this.$store.state.userInfo.username,
				addGroupId: -1,
				addGroupMessage: "我是" + this.$store.state.userInfo.username
			}
		},
		filters: {

		},
		methods: {
			toBlue(str, key) {
				var reg = new RegExp((`(${key})`), "gm");
				var replace = "<span style='color:#9ea1ff'>$1</span>"
				return str.replace(reg, replace);
			},
			back() {
				uni.navigateBack({})
			},
			search() {
				if (this.tab === 1) {
					this.person_index = 1
					this.loadKeyword_ren = this.searchKeywords_ren
					this.person_list = []
					this.person_message = "加载中..."
					this.getPersonList(this.loadKeyword_ren)
				}
				if (this.tab === 2) {
					this.group_index = 1
					this.loadKeyword_qun = this.searchKeywords_qun
					this.group_list = []
					this.group_message = "加载中..."
					this.getGroupList(this.loadKeyword_qun)
				}
			},
			getPersonList(keywords) {
				this.$request.TokenRequest({
					url: "/user/search"
				}, {
					keyword: keywords,
					index: this.person_index
				}).then(res => {
					if (res.state === 200) {
						this.person_list = this.person_list.concat(res.data)
						this.person_index++
						if (res.message === "false") {
							this.person_message = "没有更多信息了"
							this.person_noMore = true
						}
					}
				})
			},
			getGroupList(keywords) {
				this.$request.TokenRequest({
					url: "/group/search"
				}, {
					keyword: keywords,
					index: this.group_index
				}).then(res => {
					if (res.state === 200) {
						this.group_list = this.group_list.concat(res.data)
						this.group_index++
						if (res.message === "false") {
							this.group_message = "没有更多信息了"
							this.group_noMore = true
						}
					}
				})
			},
			addFriend() {
				// console.log(id)
				this.$request.TokenRequest({
					url: "/friend/requestToAdd"
				}, {
					fromId: this.$store.state.userInfo.id,
					toId: this.addFriendId,
					message: this.addFriendMessage,
					isDeal: 0,
				}).then(res => {
					if (res.state === 200) {
						this.addFriendId = -1
						uni.showToast({
							icon: "success",
							title: "请求发送成功!"
						})
					} else {
						uni.showToast({
							icon: "none",
							title: res.message
						})
					}
				})
			},
			watchFriend(id) {
				// console.log(id)
				uni.navigateTo({
					url: '../informationPage/informationPage?id=' + id,
					fail(e) {
						console.log(e)
					}
				})
			},
			watchGroup(id) {
				uni.navigateTo({
					url: "../groupInfo/groupInfo?gid=" + id
				})
			},
			joinGroup(gid) {
				this.$request.TokenRequest({
					url: "/group/addGroup"
				}, {
					gid: gid,
					uid: this.$store.state.userInfo.id
				}).then(res => {
					if (res.state === 200) {
						uni.showToast({
							icon: "success",
							title: "加入成功!"
						})
					} else {
						uni.showToast({
							icon: "none",
							title: res.message
						})
					}
				})
			},
			applyGroup() {
				this.$request.TokenRequest({
					url: "/group/requestToAdd"
				}, {
					uid: this.$store.state.userInfo.id,
					gid: this.addGroupId,
					message: this.addGroupMessage,
					isDeal: 0
				}).then(res => {
					if (res.state === 200) {
						this.addGroupId = -1
						uni.showToast({
							icon: "success",
							title: "申请发送成功!"
						})
					} else {
						uni.showToast({
							icon: "none",
							title: res.message
						})
					}
				})
			}
		},
		onReachBottom() {
			if (this.tab === 1) {
				if (this.person_noMore === false)
					this.getPersonList(this.loadKeyword_ren)
			} else if (this.tab === 2) {
				if (this.group_noMore === false)
					this.getGroupList(this.loadKeyword_qun)
			}


		}
	}
</script>

<style lang="scss" scoped>
	.search {
		width: 90%;
		margin: 20rpx auto;
		background-color: #F5F5F5
	}

	.searchbar {}

	.tab {
		margin: 0 auto;
		line-height: 120rpx;

		.ren {
			border-radius: 10rpx 0 0 10rpx;
		}

		.qun {
			border-radius: 0 10rpx 10rpx 0;
		}

		.btn {
			font-size: 30rpx;
			padding: 5rpx 25rpx;
		}

		.isActive {
			color: #6869a8;
			background-color: #FFF;
			border: 3rpx solid #474881;
		}

		.noActive {
			color: #FFF;
			background-color: #474881;
			border: 3rpx solid #474881;
		}
	}

	.list {
		.noInfo {
			padding: 30rpx 0;
			color: #C1C1C1;
			text-align: center;
		}

		.card {
			display: flex;
			height: 150rpx;
			align-items: center;
			border-bottom: 2rpx solid #F5F5F5;

			.image {
				margin-left: 25rpx;
				margin-right: 15rpx;
				height: 110rpx;
				width: 110rpx;
				border-radius: 15rpx;
			}

			.text {
				margin: 0 7rpx;
				font-size: 34rpx;
			}

			.username {
				display: inline-block;
				max-width: 280rpx;
				overflow: hidden;
				overflow-y: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.id {
				display: inline-block;
				color: #858786;
			}

			.button {
				font-size: 32rpx;
				position: absolute;
				right: 30rpx;
				border: 1rpx solid #BABABA;
				border-radius: 15rpx;
				padding: 8rpx 28rpx;
			}

			.no {
				font-size: 28rpx;
				position: absolute;
				right: 30rpx;
				color: #BABABA;
			}

			.button_hover {
				background-color: #F5F5F5;
			}

			.des {
				color: #858786;
				font-size: 27rpx;
				max-width: 300rpx;
				overflow-y: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

	}
</style>
